﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Transitions;

namespace UIT_CV_Management
{
    public partial class LookupForm : Form
    {
        //SQL Begin
        String Query = "";
        String OrgBeginQuery = "SELECT Users.Username FROM Users\n" +
                        "LEFT JOIN LyLichKhoaHoc\n" +
                            "ON Users.MaLyLichKhoaHoc = LyLichKhoaHoc.MaLyLichKhoaHoc\n";
        String OrgLyLichQuery = "LEFT JOIN LyLichSoLuoc\n" +
                            "ON LyLichKhoaHoc.MaLyLichSoLuoc = LyLichSoLuoc.MaLyLichSoLuoc\n" +
                        "LEFT JOIN ThongTinPhu\n" +
                            "ON LyLichSoLuoc.MaThongTinPhu = ThongTinPhu.MaThongTinPhu\n" +
                        "LEFT JOIN NgheNghiep\n" +
                            "ON LyLichSoLuoc.MaNgheNghiep = NgheNghiep.MaNgheNghiep\n";
        String OrgQuaTrinhDaoTaoQuery = "LEFT JOIN QuaTrinhDaoTao\n" +
                            "ON LyLichKhoaHoc.MaQuaTrinhDaoTao = QuaTrinhDaoTao.MaQuaTrinhDaoTao\n";
        String OrgDaoTaoDaiHocQuery = "LEFT JOIN DaoTaoDaiHoc\n" +
                            "ON QuaTrinhDaoTao.MaDaoTaoDaiHoc = DaoTaoDaiHoc.MaDaoTaoDaiHoc\n" +
                        "LEFT JOIN ChiTietNoiHoc\n" +
                            "ON DaoTaoDaiHoc.MaNoiHoc = ChiTietNoiHoc.MaChiTietNoiHoc\n" +
                        "LEFT JOIN CheDoHoc\n" +
                            "ON ChiTietNoiHoc.MaCheDoHoc = CheDoHoc.MaCheDoHoc\n" +
                        "LEFT JOIN TieuLuan\n" +
                            "ON ChiTietNoiHoc.MaTieuLuan = TieuLuan.MaTieuLuan\n" +
                        "LEFT JOIN TotNghiep\n" +
                            "ON ChiTietNoiHoc.MaTotNghiep = TotNghiep.MaTotNghiep\n" +
                        "LEFT JOIN MonThiTotNghiep\n" +
                            "ON TotNghiep.MaMonThiTotNghiep = MonThiTotNghiep.MaMonThiTotNghiep\n";
        String OrgDaoTaoTrenDaiHocQuery = "LEFT JOIN DaoTaoTrenDaiHoc\n" +
                            "ON QuaTrinhDaoTao.MaDaoTaoTrenDaiHoc = DaoTaoTrenDaiHoc.MaDaoTaoTrenDaiHoc\n" +
                        "LEFT JOIN NghienCuu\n" +
                            "ON DaoTaoTrenDaiHoc.MaNghienCuu = NghienCuu.MaNghienCuu\n" +
                        "LEFT JOIN DiaDiemNghienCuu\n" +
                            "ON NghienCuu.MaDiaDiem = DiaDiemNghienCuu.MaDiaDiem\n" +
                        "LEFT JOIN ThucTap\n" +
                            "ON DaoTaoTrenDaiHoc.MaThucTap = ThucTap.MaThucTap\n" +
                        "LEFT JOIN DiaDiemThucTap\n" +
                            "ON ThucTap.MaDiaDiem = DiaDiemThucTap.MaDiaDiem\n";
        String OrgNgoaiNguHocHamQuery = "LEFT JOIN DaoTaoNgoaiNgu\n" +
                            "ON QuaTrinhDaoTao.MaDaoTaoNgoaiNgu = DaoTaoNgoaiNgu.MaDaoTaoNgoaiNgu\n" +
                        "LEFT JOIN DanhGiaTrinhDoNgoaiNgu\n" +
                            "ON DaoTaoNgoaiNgu.MaDanhGiaTrinhDo = DanhGiaTrinhDoNgoaiNgu.MaDanhGiaTrinhDo\n" +
                        "LEFT JOIN DaoTaoHocHam\n" +
                            "ON QuaTrinhDaoTao.MaDaoTaoHocHam = DaoTaoHocHam.MaDaoTaoHocHam\n" +
                        "LEFT JOIN HocHam\n" +
                            "ON DaoTaoHocHam.MaTenHocHam = HocHam.MaTenHocHam\n";
        String OrgHoatDongKHKTQuery = "LEFT JOIN HoatDongKHKT\n" +
                            "ON LyLichKhoaHoc.MaHoatDongKHKT = HoatDongKHKT.MaHoatDongKHKT\n" +
                        "LEFT JOIN QuaTrinhHoatDongKHKT\n" +
                            "ON HoatDongKHKT.MaQuaTrinhHDKHKT = QuaTrinhHoatDongKHKT.MaQuaTrinhHDKHKT\n" +
                        "LEFT JOIN HoiNghiKHKT\n" +
                            "ON HoatDongKHKT.MaHoiNghiKHKT = HoiNghiKHKT.MaHoiNghiKHKT\n" +
                        "LEFT JOIN BaoCaoCanBo\n" +
                            "ON HoiNghiKHKT.MaBaoCaoCanBo = BaoCaoCanBo.MaBaoCaoCanBo\n" +
                        "LEFT JOIN KhenThuong\n" +
                            "ON HoatDongKHKT.MaKhenThuong = KhenThuong.MaKhenThuong\n";
        String OrgWHEREQuery = "WHERE ";

        List<String> TableQuery = new List<string>();

        Boolean isValidNamSinh = true;
        Boolean isValidNamDeAn = true;
        Boolean isValidNamHoiNghi = true;
        Boolean isValidTDHNamHoc = true;
        Boolean isValidDHNamHoc = true;

        public Int32 Index = 0;
        CheckBox gblCheckBox = null;
        Label gblLabel = null;
        GroupBox gblGroupBox = null;
        public LookupForm()
        {
            InitializeComponent();

            //all control in information panel
            foreach (Control ctrl in pnlInformation.Controls)
                ctrl.Click += new System.EventHandler(this.InformationItem_Click);

            //all control in each groupbox have TextChanged Event
            foreach (Control control in this.Controls)
                if (control.GetType() == typeof(GroupBox))
                    foreach (Control ctrl in control.Controls)
                        if (ctrl.GetType() == typeof(TextBox) || ctrl.GetType() == typeof(ComboBox))
                            ctrl.TextChanged += new System.EventHandler(this.Value_Changed);
            InitLanguage();
            tlplResult.Padding = new Padding(0, 0, SystemInformation.VerticalScrollBarWidth, 0);
        }

        /// <summary>
        /// Initialize the language of form
        /// </summary>
        private void InitLanguage()
        {
            this.Text = Language._("LOOKUP_FORM_TITLE");
            this.lblTitle.Text = Language._("LOOKUP_FORM_TITLE_LBL");
            this.lblLyLich.Text = Language._("LOOKUP_FORM_LY_LICH_LBL");
            this.lblDaoTaoDaiHoc.Text = Language._("LOOKUP_FORM_DAO_TAO_DAI_HOC_LBL");
            this.lblDaoTaoTrenDaiHoc.Text = Language._("LOOKUP_FORM_DAO_TAO_TREN_DAI_HOC_LBL");
            this.lblNgoaiNguHocHam.Text = Language._("LOOKUP_FORM_NGOAI_NGU_HOC_HAM_LBL");
            this.lblHoatDongKHKT.Text = Language._("LOOKUP_FORM_HOAT_DONG_KHKT_LBL");
            this.gbxLyLich.Text = Language._("LOOKUP_FORM_LY_LICH_GBX");
            this.lblLLHoTen.Text = Language._("LOOKUP_FORM_HO_TEN_LBL");
            this.lblLLPhai.Text = Language._("LOOKUP_FORM_PHAI_LBL");
            this.lblLLNamSinh.Text = Language._("LOOKUP_FORM_NAM_SINH_LBL");
            this.lblLLDiaChi.Text = Language._("LOOKUP_FORM_DIA_CHI_LBL");
            this.cbxLLPhai.Items.Clear();
            this.cbxLLPhai.Items.Add(Language._("LOOKUP_FORM_PHAI_NAM_TEXT"));
            this.cbxLLPhai.Items.Add(Language._("LOOKUP_FORM_PHAI_NU_TEXT"));
            this.gbxDaoTaoDaiHoc.Text = Language._("LOOKUP_FORM_DAO_TAO_DAI_HOC_GBX");
            this.lblDHTenTruong.Text = Language._("LOOKUP_FORM_DH_TEN_TRUONG_LBL");
            this.lblDHTenTP.Text = Language._("LOOKUP_FORM_DH_TEN_TP_LBL");
            this.lblDHNamHoc.Text = Language._("LOOKUP_FORM_DH_NAM_HOC_LBL");
            this.chbxDHLuanAn.Text = Language._("LOOKUP_FORM_DH_LUAN_AN_CHBX");
            this.chbxDHThiTotNghiep.Text = Language._("LOOKUP_FORM_DH_THI_TOT_NGHIEP_CHBX");
            this.gbxDaoTaoTrenDaiHoc.Text = Language._("LOOKUP_FORM_DAO_TAO_TREN_DAI_HOC_GBX");
            this.lblTDHTenTruong.Text = Language._("LOOKUP_FORM_TDH_TEN_TRUONG");
            this.lblTDHTenNuoc.Text = Language._("LOOKUP_FORM_TDH_TEN_NUOC");
            this.lblTDHNamHoc.Text = Language._("LOOKUP_FORM_TDH_NAM_HOC");
            this.chbxTDHThucTap.Text = Language._("LOOKUP_FORM_TDH_THUC_TAP_CHBX");
            this.chbxTDHNghienCuu.Text = Language._("LOOKUP_FORM_TDH_NGHIEN_CUU_CHBX");
            this.gbxNgoaiNguHocHam.Text = Language._("LOOKUP_FORM_NGOAI_NGU_HOC_HAM_GBX");
            this.lblNNHHTenHocHam.Text = Language._("LOOKUP_FORM_NNHH_TEN_HOC_HAM_LBL");
            this.lblNNHHTenNgoaiNgu.Text = Language._("LOOKUP_FORM_NNHH_TEN_NGOAI_NGU_LBL");
            this.gbxHoatDongKHKT.Text = Language._("LOOKUP_FORM_HOAT_DONG_KHKT_GBX");
            this.lblHDKHKTTenDeAn.Text = Language._("LOOKUP_FORM_HDKHKT_TEN_DE_AN_LBL");
            this.lblHDKHKTTenHoiNghi.Text = Language._("LOOKUP_FORM_HDKHKT_TEN_HOI_NGHI_LBL");
            this.lblHDKHKTNamDeAn.Text = Language._("LOOKUP_FORM_HDKHKT_NAM_DE_AN_LBL");
            this.lblHDKHKTNamHoiNghi.Text = Language._("LOOKUP_FORM_HDKHKT_NAM_HOI_NGHI_LBL");
            this.gbxKetQua.Text = Language._("LOOKUP_FORM_KET_QUA_GBX");
        }

        /// <summary>
        /// All information label and checkbox click event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void InformationItem_Click(object sender, EventArgs e)
        {
            Control currentcontrol = (Control)sender;
            Int32 LastIndex = Index;
            Index = currentcontrol.Top / 31;

            if (gblLabel == null) gblLabel = lblLyLich;
            if (gblCheckBox == null) gblCheckBox = chbxLyLich;
            if (gblGroupBox == null) gblGroupBox = gbxLyLich;

            if (Index != LastIndex && gblLabel != null && gblCheckBox != null)
            {
                gblCheckBox.BackColor = Color.White;
                gblLabel.BackColor = Color.White;
                gblLabel.ForeColor = Color.Black;
            }

            Control ctrlOnScreen = null, ctrlOffScreen;
            ctrlOffScreen = gblGroupBox;
            switch (Index)
            {
                case 0:
                    gblCheckBox = chbxLyLich;
                    gblLabel = lblLyLich;
                    ctrlOnScreen = gbxLyLich;
                    gblGroupBox = gbxLyLich;
                    break;
                case 1:
                    gblCheckBox = chbxDaoTaoDaiHoc;
                    gblLabel = lblDaoTaoDaiHoc;
                    ctrlOnScreen = gbxDaoTaoDaiHoc;
                    gblGroupBox = gbxDaoTaoDaiHoc;
                    break;
                case 2:
                    gblCheckBox = chbxDaoTaoTrenDaiHoc;
                    gblLabel = lblDaoTaoTrenDaiHoc;
                    ctrlOnScreen = gbxDaoTaoTrenDaiHoc;
                    gblGroupBox = gbxDaoTaoTrenDaiHoc;
                    break;
                case 3:
                    gblCheckBox = chbxNgoaiNguHocHam;
                    gblLabel = lblNgoaiNguHocHam;
                    ctrlOnScreen = gbxNgoaiNguHocHam;
                    gblGroupBox = gbxNgoaiNguHocHam;
                    break;
                case 4:
                    gblCheckBox = chbxHoatDongKHKT;
                    gblLabel = lblHoatDongKHKT;
                    ctrlOnScreen = gbxHoatDongKHKT;
                    gblGroupBox = gbxHoatDongKHKT;
                    break;
            }

            if (Index != LastIndex && gblLabel != null && gblCheckBox != null && ctrlOnScreen != null)
            {
                gblCheckBox.BackColor = Color.CornflowerBlue;
                gblLabel.BackColor = Color.CornflowerBlue;
                gblLabel.ForeColor = Color.White;

                foreach(Control control in ctrlOnScreen.Controls)
                    if (control.TabIndex == 5)
                        control.Focus();
                
                ctrlOnScreen.BringToFront();
                ctrlOffScreen.SendToBack();

                Transition t = new Transition(new TransitionType_EaseInEaseOut(400));
                t.add(ctrlOnScreen, "Left", 218);
                t.add(ctrlOffScreen, "Left", this.Width);
                t.run();
            }
        }

        /// <summary>
        /// TextChanged Event for all textbox
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Value_Changed(object sender, EventArgs e)
        {
            tlplResult.Visible = false;
            if (sender.GetType() == typeof(TextBox))
            {
                TextBox Sender = (TextBox)sender;
                GroupBox Parent = (GroupBox)Sender.Parent;
                if (Parent == gbxLyLich)
                    chbxLyLich.Checked = true;
                if (Parent == gbxDaoTaoDaiHoc)
                    chbxDaoTaoDaiHoc.Checked = true;
                if (Parent == gbxDaoTaoTrenDaiHoc)
                    chbxDaoTaoTrenDaiHoc.Checked = true;
                if (Parent == gbxNgoaiNguHocHam)
                    chbxNgoaiNguHocHam.Checked = true;
                if (Parent == gbxHoatDongKHKT)
                    chbxHoatDongKHKT.Checked = true;
            }

            if (sender.GetType() == typeof(ComboBox))
            {
                ComboBox Sender = (ComboBox)sender;
                GroupBox Parent = (GroupBox)Sender.Parent;
                if (Parent == gbxLyLich)
                    chbxLyLich.Checked = true;
                if (Parent == gbxDaoTaoDaiHoc)
                    chbxDaoTaoDaiHoc.Checked = true;
                if (Parent == gbxDaoTaoTrenDaiHoc)
                    chbxDaoTaoTrenDaiHoc.Checked = true;
                if (Parent == gbxNgoaiNguHocHam)
                    chbxNgoaiNguHocHam.Checked = true;
                if (Parent == gbxHoatDongKHKT)
                    chbxHoatDongKHKT.Checked = true;
            }

            if (sender.GetType() == typeof(CheckBox))
            {
                CheckBox Sender = (CheckBox)sender;
                
            }
            tmrQuery.Stop();
            tmrQuery.Start();
        }

        private void tbxNamSinh_Validating(object sender, CancelEventArgs e)
        {
            Regex rgx = new Regex(@"^[0-9]+$");
            if (tbxLLNamSinh.Text != "")
                if (rgx.IsMatch(tbxLLNamSinh.Text))
                {
                    isValidNamSinh = true;
                    tbxLLNamSinh.BackColor = Color.White;
                }
                else
                {
                    isValidNamSinh = false;
                    tbxLLNamSinh.BackColor = Color.Tomato;
                }
            else
            {
                isValidNamSinh = true;
                tbxLLNamSinh.BackColor = Color.White;
            }
        }

        private void LyLichQueryBuilder(String _HoTen, String _Phai, String _NamSinh, String _DiaChi)
        {
            AddStatement("LyLichSoLuoc.HoTen", "LIKE", _HoTen);
            AddStatement("LyLichSoLuoc.Phai", "=", _Phai);
            if (isValidNamSinh)
                AddStatement("Year(LyLichSoLuoc.NgaySinh)", "=", _NamSinh);
            AddStatement("LyLichSoLuoc.NoiSinh", "LIKE", _DiaChi);
        }

        private void DaoTaoDaiHocQueryBuilder(String _TenTruong, String _TenTP, String _NamHoc, 
            Boolean _LuanAn, Boolean _TotNghiep)
        {
            AddStatement("ChiTietNoiHoc.TenTruong", "LIKE", _TenTruong);
            AddStatement("ChiTietNoiHoc.TenThanhPho", "=", _TenTP);
            AddStatement("Year(ChiTietNoiHoc.ThoiGianBatDau)", "<=", _NamHoc);
            AddStatement("Year(ChiTietNoiHoc.ThoiGianKetThuc)", ">=", _NamHoc);
            if (_LuanAn) AddStatement("ChiTietNoiHoc.MaTieuLuan", "!=", "NULL");
            if (_TotNghiep) AddStatement("ChiTietNoiHoc.MaTotNghiep", "!=", "NULL");
        }

        private void DaoTaoTrenDaiHocQueryBuilder(String _TenTruong, String _TenNuoc, String _NamHoc,
            Boolean _ThucTap, Boolean _NghienCuu)
        {
            if (chbxTDHThucTap.Checked)
            {
                AddStatement("DiaDiemThucTap.TenTruong", "LIKE", _TenTruong);
                AddStatement("DiaDiemThucTap.TenNuoc", "=", _TenNuoc);
                AddStatement("Year(ThucTap.NgayBatDau)", "<=", _NamHoc);
                AddStatement("Year(ThucTap.NgayKetThuc)", ">=", _NamHoc);
            }
            if (chbxTDHNghienCuu.Checked)
            {
                AddStatement("DiaDiemNghienCuu.TenTruong", "LIKE", _TenTruong);
                AddStatement("DiaDiemNghienCuu.TenNuoc", "=", _TenNuoc);
                AddStatement("Year(NghienCuu.NgayBatDau)", "<=", _NamHoc);
                AddStatement("Year(NghienCuu.NgayKetThuc)", ">=", _NamHoc);
            }
        }

        private void NgoaiNguHocHamQueryBuilder(String _TenNgoaiNgu, String _TenHocHam)
        {
            AddStatement("DaoTaoNgoaiNgu.TenNgoaiNgu", "=", _TenNgoaiNgu);
            AddStatement("HocHam.TenHocHam", "=", _TenHocHam);
        }

        private void HoatDongKHKTQueryBuilder(String _TenDeAn, String _TenHoiNghi,
            String _NamDeAn, String _NamHoiNghi)
        {
            AddStatement("QuaTrinhHoatDongKHKT.TenDeAn", "LIKE", _TenDeAn);
            AddStatement("Year(QuaTrinhHoatDongKHKT.ThoiGian)", "=", _NamDeAn);
            AddStatement("HoiNghiKHKT.TenHoiNghi", "LIKE", _TenHoiNghi);
            AddStatement("Year(HoiNghiKHKT.ThoiGian)", "=", _NamHoiNghi);
        }

        private void AddStatement(String _Column, String _Comparison, String _Value)
        {
            if (_Value != "")
            {
                if (Query.Substring(Query.Length - 6) != "WHERE ")
                    if (Query.Substring(Query.Length - 6) != "AND ")
                        Query += "AND ";
                if (_Comparison == "LIKE")
                    _Value = "%" + _Value + "%";
                if (_Comparison != ">=" || _Comparison != "<=" || _Comparison != "<" || _Comparison != ">")
                {
                    if (_Value != "NULL")
                        Query += _Column + " " + _Comparison + " N'" + _Value + "' ";
                    else if (_Comparison == "=")
                        Query += _Column + " " + _Comparison + " " + _Value + " ";
                }
                else
                    Query += _Column + " " + _Comparison + " " + _Value + " ";
            }
        }

        private void tmrQuery_Tick(object sender, EventArgs e)
        {
            this.Validate();
            if (!isValidDHNamHoc || !isValidNamDeAn || !isValidNamHoiNghi
                || !isValidNamSinh || !isValidTDHNamHoc)
                return;
            Query = OrgBeginQuery;

            if (chbxLyLich.Checked)
                Query += OrgLyLichQuery;

            if (chbxDaoTaoDaiHoc.Checked || chbxDaoTaoTrenDaiHoc.Checked
                || chbxNgoaiNguHocHam.Checked || chbxHoatDongKHKT.Checked)
                Query += OrgQuaTrinhDaoTaoQuery;

            if (chbxDaoTaoDaiHoc.Checked)
                Query += OrgDaoTaoDaiHocQuery;

            if (chbxDaoTaoTrenDaiHoc.Checked)
                Query += OrgDaoTaoTrenDaiHocQuery;

            if (chbxNgoaiNguHocHam.Checked)
                Query += OrgNgoaiNguHocHamQuery;

            if (chbxHoatDongKHKT.Checked)
                Query += OrgHoatDongKHKTQuery;

            Query += OrgWHEREQuery;

            if (chbxLyLich.Checked)
            {
                String HoTen = tbxLLHoTen.Text;
                String Phai = cbxLLPhai.SelectedIndex.ToString();
                
                if (Phai == "-1") Phai = "";
                String NamSinh = tbxLLNamSinh.Text;
                String DiaChi = tbxLLDiaChi.Text;
                LyLichQueryBuilder(HoTen, Phai, NamSinh, DiaChi);
            }

            if (chbxDaoTaoDaiHoc.Checked)
            {
                String TenTruong = tbxDHTenTruong.Text;
                String TenTP = tbxDHTenTP.Text;
                String NamHoc = tbxDHNamHoc.Text;
                Boolean LuanAn = chbxDHLuanAn.Checked;
                Boolean TotNghiep = chbxDHThiTotNghiep.Checked;
                DaoTaoDaiHocQueryBuilder(TenTruong, TenTP, NamHoc, LuanAn, TotNghiep);
            }

            if (chbxDaoTaoTrenDaiHoc.Checked)
            {
                String _TenTruong = tbxDHTenTruong.Text;
                String _TenNuoc = tbxDHTenTP.Text;
                String _NamHoc = tbxDHNamHoc.Text;
                Boolean _ThucTap = chbxTDHThucTap.Checked;
                Boolean _NghienCuu = chbxTDHNghienCuu.Checked;
                DaoTaoTrenDaiHocQueryBuilder(_TenTruong, _TenNuoc, _NamHoc, _ThucTap, _NghienCuu);
            }

            if (chbxNgoaiNguHocHam.Checked)
            {
                String TenNgoaiNgu = tbxNNHHTenNgoaiNgu.Text;
                String TenHocHam = tbxNNHHTenHocHam.Text;
                NgoaiNguHocHamQueryBuilder(TenNgoaiNgu, TenHocHam);
            }

            if (chbxHoatDongKHKT.Checked)
            {
                String TenDeAn = tbxHDKHKTTenDeAn.Text;
                String NamDeAn = tbxHDKHKTNamDeAn.Text;
                String TenHoiNghi = tbxHDKHKTTenHoiNghi.Text;
                String NamHoiNghi = tbxHDKHKTNamHoiNghi.Text;
                HoatDongKHKTQueryBuilder(TenDeAn, TenHoiNghi, NamDeAn, NamHoiNghi);
            }

            QueryAndGetResult();

            tmrQuery.Stop();
        }

        private void tbxHDKHKTNamDeAn_Validating(object sender, CancelEventArgs e)
        {
            Regex rgx = new Regex(@"^[0-9]+$");
            if (tbxHDKHKTNamDeAn.Text != "")
                if (rgx.IsMatch(tbxHDKHKTNamDeAn.Text))
                {
                    isValidNamDeAn = true;
                    tbxHDKHKTNamDeAn.BackColor = Color.White;
                }
                else
                {
                    isValidNamDeAn = false;
                    tbxHDKHKTNamDeAn.BackColor = Color.Tomato;
                }
            else
            {
                isValidNamDeAn = true;
                tbxHDKHKTNamDeAn.BackColor = Color.White;
            }
        }

        private void tbxHDKHKTNamHoiNghi_Validating(object sender, CancelEventArgs e)
        {
            Regex rgx = new Regex(@"^[0-9]+$");
            if (tbxHDKHKTNamHoiNghi.Text != "")
                if (rgx.IsMatch(tbxHDKHKTNamHoiNghi.Text))
                {
                    isValidNamHoiNghi = true;
                    tbxHDKHKTNamHoiNghi.BackColor = Color.White;
                }
                else
                {
                    isValidNamHoiNghi = false;
                    tbxHDKHKTNamHoiNghi.BackColor = Color.Tomato;
                }
            else
            {
                isValidNamHoiNghi = true;
                tbxHDKHKTNamHoiNghi.BackColor = Color.White;
            }
        }

        private void tbxTDHNamHoc_Validating(object sender, CancelEventArgs e)
        {
            Regex rgx = new Regex(@"^[0-9]+$");
            if (tbxTDHNamHoc.Text != "")
                if (rgx.IsMatch(tbxTDHNamHoc.Text))
                {
                    isValidTDHNamHoc = true;
                    tbxTDHNamHoc.BackColor = Color.White;
                }
                else
                {
                    isValidTDHNamHoc = false;
                    tbxTDHNamHoc.BackColor = Color.Tomato;
                }
            else
            {
                isValidTDHNamHoc = true;
                tbxTDHNamHoc.BackColor = Color.White;
            }
        }

        private void tbxDHNamHoc_Validating(object sender, CancelEventArgs e)
        {
            Regex rgx = new Regex(@"^[0-9]+$");
            if (tbxDHNamHoc.Text != "")
                if (rgx.IsMatch(tbxDHNamHoc.Text))
                {
                    isValidDHNamHoc = true;
                    tbxDHNamHoc.BackColor = Color.White;
                }
                else
                {
                    isValidDHNamHoc = false;
                    tbxDHNamHoc.BackColor = Color.Tomato;
                }
            else
            {
                isValidDHNamHoc = true;
                tbxDHNamHoc.BackColor = Color.White;
            }
        }

        private void QueryAndGetResult()
        {
            QueryResult result = Database.shareDatabase.Query(Query);
            if (result.ReturnCode == 200)
            {
                if (tlplResult.Controls.Count > 0) tlplResult.Controls.Clear();
                foreach (DataRow dr in result.Table.Rows)
                {
                    String Username = dr["Username"].ToString();
                    User usr = new User(Username);
                    ContactView ctvw = new ContactView();
                    LyLichSoLuoc llsl = usr.LyLichKhoaHoc.lyLichSoLuoc;
                    ctvw.UserName = llsl.hoTen;
                    //ctvw.Position = llsl.ngheNghiep.chucVu;
                    ctvw.Position = llsl.getNgheNghiep().chucVu;
                    ctvw.Address = llsl.noiSinh;
                    ctvw.Anchor = (AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top);
                    ctvw.Click += new System.EventHandler(this.ContactView_Click);
                    foreach (Control ctrl in ctvw.Controls)
                        ctrl.Click += new System.EventHandler(this.ContactView_Click);
                    tlplResult.Controls.Add(ctvw);
                }
                tlplResult.Visible = true;
            }
        }
        
        private void ContactView_Click(object sender, EventArgs e)
        {
            Control ctvw = (Control)sender;
            if (sender.GetType() == typeof(ContactView))
            {
                if (ctvw.BackColor != Color.CornflowerBlue)
                {
                    ctvw.BackColor = Color.CornflowerBlue;
                    ctvw.ForeColor = Color.White;
                }
                else
                {
                    ctvw.BackColor = Color.White;
                    ctvw.ForeColor = Color.Black;
                }
            }
            else
            {
                if (ctvw.Parent.BackColor != Color.CornflowerBlue)
                {
                    ctvw.Parent.BackColor = Color.CornflowerBlue;
                    ctvw.Parent.ForeColor = Color.White;
                }
                else
                {
                    ctvw.Parent.BackColor = Color.White;
                    ctvw.Parent.ForeColor = Color.Black;
                }
            }
        }
    }
}
